var posNumber = "";
var posAmount = "";
var posDetail = "";
var searchTime = "";
var isOperating = false;

//用户管理加载
function load() {
	searchTime = $('#searchTransactionTime').val();
	
	var message = $('#message').val();
	if (message != "" && message != null) {
		alert(message);
	}
	
	if($("#showData").val()=="true"){
		$("#boxShowData").attr("checked","checked");
	}
	search();
}

function sortByCondition(condition){
	$("#boxSortBySales").val($('#boxSortBySales').is(":checked"));
	$("#boxShowData").val($('#boxShowData').is(":checked"));
	$("#currentPage").val(1);
	$("#myForm").submit();
}


// 查询用户
function search() {
	showLoading();
	var currentPage = $('#gotoPage').val();
	var beginTime = $('#beginTime').val();
	var siteType = $('#siteType').val();
	var siteName = $('#siteName').val();
	var square = $('#square').val();
	var showData = $('#boxShowData').attr("checked");
	$.ajax( {
		type : "GET",
		contentType : "application/json",
		data : {
			currentPage : currentPage,
			beginTime : beginTime,
			siteType : siteType,
			square : square,
			siteName : encodeURI(siteName),
			showData : showData
		},
		dataType : 'json',
		url : "ajaxPosSiteList",
		async : true,
		success : function(msgs) {
			$('#leftFY').show();
			$('#rightFY').show();
			$('#myTable tr:not(:first)').empty();
			var siteList = msgs.siteList;
			
			$('#curpage').html(msgs.currentPage);
			$('#allpage').html(msgs.maxPage);
			if (msgs.maxPage == 1) {
				$('#leftFY').hide();
				$('#rightFY').hide();
			}
			if (msgs.currentPage == 1) {
				$('#leftFY').hide();
			}
			if (msgs.currentPage == msgs.maxPage) {
				$('#rightFY').hide();
			}
			if(siteList.length==0){
				$('#divfanye').hide();
			}
			// 获取语言包
			var language = msgs.language;
			// 获取当前登录用户名
			var recentUserName = msgs.recentUserName;
			// 获取删除权限
			var isDelete = msgs.isDelete;
			if(siteList.length==0){
				$('#myTable')
				.append("<tr><td class='xytd' colspan=10>没有您要查询的数据！</tr>");
			}else{
			for ( var i = 0; i < siteList.length; i++) {
				
				var siteType = siteList[i].siteType;
				var type = ""
				if(siteType == "0"){
					type = "根节点";
				}else if(siteType == "100"){
					type = "集团";
				}else if(siteType == "200"){
					type = "地域";
				}else if(siteType == "300"){
					type = "广场";
				}else if(siteType == "400"){
					type = "区域";
				}else if(siteType == "500"){
					type = "楼层";
				}else if(siteType == "600"){
					type = "店铺";
				}else if(siteType == "700"){
					type = "楼道";
				}else{
					type = "商圈";
				}
				
				var tempSite = siteList[i];
				var inputType = tempSite.inputType;
				var transactionNumber = tempSite.transactionNumber;
				var salesAmount = tempSite.salesAmount;
				var tempTime = new Date(tempSite.transactionTime);
				var mouth = tempTime.getMonth()+1;
				var transactionTime = tempTime.getFullYear()+"-"+mouth+"-"+tempTime.getDate();
				var recordNum = tempSite.recordNum;
				var posType = tempSite.inputType;
				var details = tempSite.details;
				if(inputType == "0"){
					transactionNumber = "-";
					salesAmount = "-";
					recordNum = "-";
					posType = "-";
					details = "-";
				}else if(inputType == "1"){
					posType = "手工录入";
				}else{
					posType = "导入";
					details = "-";
				}
				
				var edit = "<a href='#' class='fontOrange' onclick='editPosData(this);"
							+ "'<span>"+"编辑"+"</span> </a>";
				
				var add =  "<a href='#' class='fontOrange' onclick='addPosData(this);"
							+ "'<span>"+"新增"+"</span> </a>";
				
				var view = "<a href='#' class='fontOrange' onclick='listPosDataDetail(&quot;"+siteList[i].siteKey+"&quot;,&quot;"+siteList[i].siteName
							+ "&quot;,&quot;"+transactionTime+"&quot;);"
							+ "'<span>"+"明细"+"</span> </a>";
				
				var del = "<a href='#' class='fontOrange' onclick='deletePosData(this);"  
							+ "'<span>"+"删除"+"</span> </a>";
				// 根据权限拼接按钮
				var anniu = "";
				var siteKey = siteList[i].siteKey;
				
				var author = $('#author').val();
				if(author != null && ""!=author){
					if(author.indexOf("修改") != -1){
						if(inputType == "0"){
							anniu+=add;
						}else if(inputType == "1"){
							anniu+=edit+del;
						}else{
							anniu+=view+del;
						}
					}
					
				}
				$('#myTable')
						.append(
								"<tr>"
										+ "<td style='display:none' class='posDataId'>"
										+ tempSite.id
										+ "</td><td>"
										+ siteKey
										+ "</td><td>"
										+ siteList[i].siteName
										+ "</td><td>"
										+ type
										+ "</td><td class='posDate' id='posDate' value='"+transactionTime+"'>"
										+ transactionTime
										+ "</td><td class='posNumber' value='"+tempSite.id+"'>"
										+ commafy(transactionNumber)
										+ "</td><td class='posAmount' value='"+tempSite.id+"'>"
										+ salesAmount
										+ "</td><td class='posRecords' value='"+tempSite.id+"'>"
										+ recordNum
										+ "</td><td class='posType' value='"+tempSite.id+"'>"
										+ posType
										+ "</td><td class='posDetail' value='"+tempSite.id+"'>"
										+ details
										+ "</td><td><div class='editdiv' value='"+tempSite.id+"'>"
										+anniu
										+ "</div></td></tr>");
			}}
			hideLoading();
		},
		error : function(mm, a, b, c, d, e) {
			hideLoading();
			myAlert("提示", "服务器好像开小差了，刷新一下试试吧！");
		}
	});

}

///分页栏
function gotopage(i) {
	showLoading();
	// 纯数字正则
	var numRule = /(^\d*$)|(^\S+\s+\S+$)/;
	var j = 1;
	var maxPage = parseInt($("#allpage").html());
	// 首页
	if (i == 0) {
		j = 1;
	}
	// 下一页
	else if (i == 1) {
		j = parseInt($("#curpage").html()) + 1;
	}
	// 上一页
	else if (i == 2) {
		j = parseInt($("#curpage").html()) - 1;
	}
	// 跳转
	else if (i == 3) {
		var gotoPage = $("#txtpages").val().trim();
		if (gotoPage == "") {
			j = 1;
		} else if (!gotoPage.match(numRule)) {
			j == 1
		} else {
			j = parseInt(gotoPage);
		}
	}
	// 尾页
	else if (i == -1) {
		j = maxPage;
	}
	if (j < 1) {
		j = 1;
	}
	if (j > maxPage) {
		j = maxPage;
	}
	$("#gotoPage").val(j);
	search();
}

function editPosData(element){

	if(!isOperating){
		isOperating = true;
	
		var editdiv = $(element).parent();
		var td = $(editdiv).parent();
		var tr = $(td).parent();
		var tdList = $(tr).children();
		var idTd = tdList[0];
		var posDataId = $(idTd).text().trim();
		var siteKeyTd = tdList[1];
		var siteId = $(siteKeyTd).text().trim();
		

		$(editdiv).children("a").remove();
		var cancelHtml = "<a onclick="+"\""+"cancelEdit(this);"+"\""+
			"class='fontBlue' href='#'><span>取消</span> </a>"
		$(editdiv).append(cancelHtml);
		var confirmHtml = "<a onclick="+"\""+"confirmEdit(this);"+"\""+
		"class='fontBlue' href='#' ><span>确定</span> </a>"
		$(editdiv).append(confirmHtml);
		
		var $posNumber = $(tdList[5]);
		var posTxt = commafyback($posNumber.text().trim());
		posNumber = posTxt;
		if(posTxt == "-"){
			posTxt = "";
		}
		$posNumber.text("");
		var inputHtml = "<input type='text' maxlength='8' style='width:50px' id='txtPosNumber' value='"+ posTxt +"' />"
		$posNumber.append(inputHtml);
		
		var $posAmount = $(tdList[6]);
		var amountTxt = $posAmount.text().trim();
		posAmount = amountTxt;
		if(amountTxt == "-"){
			amountTxt = "";
		}
		$posAmount.text("");
		var inputHtml = "<input type='text' maxlength='12' style='width:50px' id='txtPosAmount' value='"+ amountTxt +"' />"
		$posAmount.append(inputHtml);
		
		var $posDetail = $(tdList[9]);
		var detailTxt = $posDetail.text().trim();
		posDetail = detailTxt;
		if(detailTxt == "-"){
			detailTxt = "";
		}
		$posDetail.text("");
		var inputHtml = "<input type='text' style='width:50px' id='txtPosdetail' maxlength='500' value='"+ detailTxt +"' />"
		$posDetail.append(inputHtml);
	}
	
}


function cancelEdit(element){

	var editdiv = $(element).parent();
	var td = $(editdiv).parent();
	var tr = $(td).parent();
	var tdList = $(tr).children();
	var idTd = tdList[0];
	var posDataId = $(idTd).text().trim();
	var siteKeyTd = tdList[1];
	var siteId = $(siteKeyTd).text().trim();
	

	$(editdiv).children("a").remove();
	var editHtml =	"<a onclick="+"\""+"editPosData(this);"+"\""+
		"class='fontOrange' href='#'><span>编辑</span> </a>"
	$(editdiv).append(editHtml);
	var deleteHtml =	"<a onclick="+"\""+"deletePosData(this);"+"\""+
	"class='fontOrange' href='#'><span>删除</span> </a>"
	$(editdiv).append(deleteHtml);
	
	var $posNumber = $(tdList[5]);
	$posNumber.children("input").remove();
	$posNumber.text(commafy(posNumber));
	
	var $posAmount = $(tdList[6]);
	$posAmount.children("input").remove();
	$posAmount.text(posAmount);
	
	var $posDetail = $(tdList[9]);
	$posDetail.children("input").remove();
	$posDetail.text(posDetail);


	
	if(isOperating){
		isOperating = false;
	}
}


function addPosData(element){

	if(!isOperating){
		isOperating = true;
	
		var editdiv = $(element).parent();
		var td = $(editdiv).parent();
		var tr = $(td).parent();
		var tdList = $(tr).children();
		var idTd = tdList[0];
		var posDataId = $(idTd).text().trim();
		var siteKeyTd = tdList[1];
		var siteId = $(siteKeyTd).text().trim();
		

		$(editdiv).children("a").remove();
		var cancelHtml = "<a onclick="+"\""+"cancelAdd(this);"+"\""+
			"class='fontBlue' href='#'><span>取消</span> </a>"
		$(editdiv).append(cancelHtml);
		var confirmHtml = "<a onclick="+"\""+"confirmAdd(this);"+"\""+
		"class='fontBlue' href='#' ><span>确定</span> </a>"
		$(editdiv).append(confirmHtml);
		
		var $posNumber = $(tdList[5]);
		var posTxt = $posNumber.text().trim();
		posNumber = posTxt;
		if(posTxt == "-"){
			posTxt = "";
		}
		$posNumber.text("");
		var inputHtml = "<input type='text' style='width:50px' maxlength='8' id='txtPosNumber' value='"+ posTxt +"' />"
		$posNumber.append(inputHtml);
		
		var $posAmount = $(tdList[6]);
		var amoutTxt = $posAmount.text().trim();
		posAmount = amoutTxt;
		if(amoutTxt == "-"){
			amoutTxt = "";
		}
		$posAmount.text("");
		var inputHtml = "<input type='text' style='width:50px' maxlength='12' id='txtPosAmount' value='"+ amoutTxt +"' />"
		$posAmount.append(inputHtml);
		
		var $posDetail = $(tdList[9]);
		var detailTxt = $posDetail.text().trim();
		posDetail = detailTxt;
		if(detailTxt == "-"){
			detailTxt = "";
		}
		$posDetail.text("");
		var inputHtml = "<input type='text' style='width:50px'  id='txtPosdetail' maxlength='500' value='"+ detailTxt +"' />"
		$posDetail.append(inputHtml);
		
		var $posRecords = $(tdList[7]);
		$posRecords.text("1");
		
		var $posType = $(tdList[8]);
		$posType.text("手动录入");
	}
	
}


function cancelAdd(element){

	var editdiv = $(element).parent();
	var td = $(editdiv).parent();
	var tr = $(td).parent();
	var tdList = $(tr).children();
	var idTd = tdList[0];
	var posDataId = $(idTd).text().trim();
	var siteKeyTd = tdList[1];
	var siteId = $(siteKeyTd).text().trim();
	

	$(editdiv).children("a").remove();
	var addHtml =	"<a onclick="+"\""+"addPosData(this);"+"\""+
		"class='fontOrange' href='#'><span>新增</span> </a>"
	$(editdiv).append(addHtml);
	
	var $posNumber = $(tdList[5]);
	$posNumber.children("input").remove();
	$posNumber.text(posNumber);
	
	var $posAmount = $(tdList[6]);
	$posAmount.children("input").remove();
	$posAmount.text(posAmount);
	
	var $posDetail = $(tdList[7]);
	$posDetail.children("input").remove();
	$posDetail.text(posDetail);
	
	var $posRecords = $(tdList[8]);
	$posRecords.text("-");
	
	var $posType = $(tdList[9]);
	$posType.text("-");
	

	
	if(isOperating){
		isOperating = false;
	}

}

function confirmAdd(element){
	var number;
	var amount;
	var details = "";
	var flag = true;
	var posDate = $("#posDate").attr("value");
	

	var editdiv = $(element).parent();
	var td = $(editdiv).parent();
	var tr = $(td).parent();
	var tdList = $(tr).children();
	var idTd = tdList[0];
	var id = $(idTd).text().trim();
	var siteKey = $(tdList[1]).text().trim();
	
	

	var $posNumber = $(tdList[5]);
	var posTxt = $posNumber.children("input").attr("value");
	posTxt = posTxt == undefined ? posNumber : posTxt;
	if(!isNumber(posTxt)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	
	var $posAmount = $(tdList[6]);
	var amoutTxt = $posAmount.children("input").attr("value");
	amoutTxt = amoutTxt == undefined ? posAmount : amoutTxt;
	if(!isDecimal(amoutTxt)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	
	var $posDetails = $(tdList[9]);
	var detailsTxt = $posDetails.children("input").attr("value");
	
	number = posTxt;
	$posNumber.children("input").remove();
	$posNumber.text(commafy(posTxt));
	
	amount = amoutTxt;
	$posAmount.children("input").remove();
	$posAmount.text(amoutTxt);
	
	details = detailsTxt;
	$posDetails.children("input").remove();
	$posDetails.text(detailsTxt);


	
	
	if(!flag){
		alert("请输入正确的数据");
		return;
	}
	
	$.ajax({
		url:"addPosData",
		type:"post",
		dataType:"json",
		async:true,
        data: { //发送给数据库的数据
		 	siteKey : siteKey,
		 	transactionNumber : number,
		 	salesAmount : amount,
		 	details : details,
		 	posDate : posDate
		 },
		success:function(msg){
			 var siteKey = msg.siteKey;
			 $(idTd).text(msg.posDataId);
			 $(editdiv).children("a").remove();
			 var editHtml = 	"<a onclick="+"\""+"editPosData(this);"+"\""+
				"class='fontOrange' href='#'><span>编辑</span> </a>"
			 $(editdiv).append(editHtml);
			var deleteHtml = "<a onclick="+"\""+"deletePosData(this);"+"\""+
				"class='fontOrange' href='#'><span>删除</span> </a>"
			$(editdiv).append(deleteHtml);
			 
			 alert("新增POS数据成功");
		},
		error: function (XMLHttpRequest, textStatus, thrownError) {
			alert('新增POS数据失败');
		}
	
	});
	
	if(isOperating){
		isOperating = false;
	}
}


function confirmEdit(element){
	var number;
	var amount;
	var details = "";
	var flag = true;
	var posDate = $("#posDate").attr("value");
	
	var editdiv = $(element).parent();
	var td = $(editdiv).parent();
	var tr = $(td).parent();
	var tdList = $(tr).children();
	var idTd = tdList[0];
	var id = $(idTd).text().trim();
	var siteKey = $(tdList[1]).text().trim();
	

	
	var $posNumber = $(tdList[5]);
	var posTxt = $posNumber.children("input").attr("value");
	posTxt = posTxt == undefined ? posNumber : posTxt;
	if(!isNumber(posTxt)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	
	var $posAmount = $(tdList[6]);
	var amountTxt = $posAmount.children("input").attr("value");
	amountTxt = amountTxt == undefined ? posAmount : amountTxt;
	if(!isDecimal(amountTxt)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	var $posDetails = $(tdList[9]);
	var detailsTxt = $posDetails.children("input").attr("value");
	
	number = posTxt;
	$posNumber.children("input").remove();
	$posNumber.text(commafy(posTxt));
	
	amount = amountTxt;
	$posAmount.children("input").remove();
	$posAmount.text(amountTxt);
	
	details = detailsTxt;
	$posDetails.children("input").remove();
	$posDetails.text(detailsTxt);


	
	
	if(!flag){
		alert("请输入正确的数据");
		return;
	}
	
	
	if(flag){

		var editdiv = $(element).parent();
		var td = $(editdiv).parent();
		var tr = $(td).parent();
		var tdList = $(tr).children();
		var idTd = tdList[0];
		var id = $(idTd).text().trim();
		var siteKey = $(tdList[1]).text().trim();

		$(editdiv).children("a").remove();
		var editHtml = 	"<a onclick="+"\""+"editPosData(this);"+"\""+
			"class='fontOrange' href='#'><span>编辑</span> </a>"
		$(editdiv).append(editHtml);
		var deleteHtml = "<a onclick="+"\""+"deletePosData(this);"+"\""+
			"class='fontOrange' href='#'><span>删除</span> </a>"
		$(editdiv).append(deleteHtml);
	
	
		
		$.ajax({
			url:"editPosData",
			type:"post",
			dataType:"json",
			async:true,
	        data: { //发送给数据库的数据
				id : id,
			 	transactionNumber : number,
			 	salesAmount : amount,
			 	details : details,
			 	posDate : posDate
			 },
			success:function(msg){
				 alert("编辑POS数据成功");
			},
			error: function (XMLHttpRequest, textStatus, thrownError) {
				alert("编辑POS数据失败");
			}
		
		});
	}
	
	
	if(isOperating){
		isOperating = false;
	}
}


function deletePosData(element){
	if(confirm("您确定要执行该操作吗？")){
		var posDate = $("#posDate").attr("value");
		
		var editdiv = $(element).parent();
		var td = $(editdiv).parent();
		var tr = $(td).parent();
		var tdList = $(tr).children();
		var idTd = tdList[0];
		var id = $(idTd).text().trim();
		var siteKey = $(tdList[1]).text().trim();
		
		$(editdiv).children("a").remove();
		var addHtml =	"<a onclick="+"\""+"addPosData(this);"+"\""+
			"class='fontOrange' href='#'><span>新增</span> </a>"
		$(editdiv).append(addHtml);
		
		for(var i=5;i<10;i++){
			$(tdList[i]).text("-");
		}
	
		
		$.ajax({
			url:"deletePosData",
			type:"post",
			dataType:"json",
			async:false,
	        data: { //发送给数据库的数据
				siteKey : siteKey,
				transactionTime : posDate
			 },
			success:function(msg){
				 var flag = msg.flag;
				 if("true"==flag){
					 alert("删除POS数据成功");
				 }else{
					 alert("删除POS数据失败");
				 }
			},
			error: function (msg) {
				alert("删除POS数据失败");
			}
		
		});
	}
}
	
function reload(){
	window.location.href = "posManager";
}

function listPosDataDetail(siteKey,siteName,transactionTime){
		var detailForm = document.getElementById("detailForm");
		$("#detailSiteKey").val(siteKey);
		$("#detailTransactionTime").val(transactionTime);
		$("#detailSiteName").val(siteName);
		detailForm.submit();
}

function loadTypes(){

	var squareId = $("#square").attr("value");
	$("#siteType option").remove();
	
	if (squareId != "-1")
	{
		$("#siteType").append('<option value="' + 300 + '"' +'selected="true"'+  ' >' + "广场" + '</option>');
		$("#siteType").append('<option value="' + 400 + '"' +'selected="true"'+  ' >' + "区域" + '</option>');
		$("#siteType").append('<option value="' + 500 + '"' +'selected="true"'+  ' >' + "楼层" + '</option>');
		$("#siteType").append('<option value="' + 600 + '"' +'selected="true"'+  ' >' + "店铺" + '</option>');
	}else{
		$("#siteType").append('<option value="' + 300 + '"' +'selected="true"'+  ' >' + "广场" + '</option>');
	}

}

function showData(){
	search();
}

function buttonAuthor(){
	var author = $('#author').val();
	if(author != null && ""!=author){
		if(author.indexOf("导入") != -1){
			$('.author_import').each(function(){
				if($(this).attr("value") == "2"){
					$(this).show();
				}
			});
			$('.author_delete').each(function(){
				if($(this).attr("value") != "0"){
					$(this).show();
				}
			});
			$('.author_view').each(function(){
				if($(this).attr("value") == "2"){
					$(this).show();
				}
			})
		}
		if(author.indexOf("修改") != -1){
			$('.author_edit').each(function(){
				if($(this).attr("value") == "1"){
					$(this).show();
				}
			});
			$('.author_add').each(function(){
				if($(this).attr("value") == "0"){
					$(this).show();
				}
			});
			$('.author_delete').each(function(){
				if($(this).attr("value") != "0"){
					$(this).show();
				}
			})
		}
	}
}

// 数字的千分位表示
function commafy(num) 
{ 
	var result = '', counter = 0;
	num = (num || 0).toString();
	for (var i = num.length - 1; i >= 0; i--) {
	counter++;
	result = num.charAt(i) + result;
	if (!(counter % 3) && i != 0) { result = ',' + result; }
	}
	return result; 
} 

// 去除千分位
function commafyback(num) 
{ 
	var x = num.split(','); 
	return parseFloat(x.join("")); 
} 


function uploadExcel(){
	var fileName = $('#txt').val();
	if(fileName==undefined || fileName == null || ""==fileName){
		alert("请先选择文件");
		return;
	}
	showLoading();
	var uploadForm = document.getElementById("uploadForm");
	uploadForm.submit();
}

